package com.sec.android.app.myfiles.util;

import android.content.Context;
import android.graphics.Rect;
import android.os.UserHandle;
import android.util.TypedValue;
import android.view.View;

import com.sec.android.app.myfiles.feature.CloudMgr;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;

/**
 * Created by eungho78.lee on 2017-05-30.
 */

public class SmartTipUtils {

    private static final String TAG = "SmartTip";

    private static final int STATUS_BAR_HEIGHT = 24;
    private static final int SMART_TIP_WIDTH_MARGIN = 35;
    private static final int SMART_TIP_HEIGHT_MARGIN = 38;

    public static boolean canShowSmartTip(Context context) {
        boolean ret = true;

        if (context == null
                || UiUtils.isFactoryMode(context)
                || AppFeatures.isAndroidForWork()
                || UiUtils.isUPSM(context)
                || AppFeatures.isKnox()) {
            ret = false;
        }
        return ret;
    }

    public static boolean isNormalOwnerMode(Context context, NavigationInfo navigationInfo) {
        boolean ret = false;
        return ret;
    }

    public static boolean planToDrawGmsSmartTip(Context context) {
        boolean ret = false;
        return ret;
    }

    public static boolean needGmsSmartTipPopup(Context context) {
        boolean ret = false;

        if (PreferenceUtils.getGmsVisitFlag(context)) // In case the user enters GMS previously.
            return ret;

        if (getSmartTipConditionNumber(context, AppConstants.SmartTipType.GMS) != -1) {
            Log.d(TAG, "needGetMoreSpaceSmartTipPopup() ] GMS Smart Tip can show.");
            ret = true;
        }
        return ret;
    }

    public static boolean needCloudSmartTipPopup(Context context, NavigationInfo navigationInfo) {
        boolean ret = false;

        CloudMgr cloudMgr = CloudMgr.getInstance(context);
        if (AppFeatures.isSupportSamsungDrive() && !PreferenceUtils.getCloudVisitFlag(context)
                && !cloudMgr.isSignedIn(FileRecord.CloudType.SamsungDrive)
                && isNormalOwnerMode(context, navigationInfo)) {
            if (getSmartTipConditionNumber(context, AppConstants.SmartTipType.CLOUD) != -1) {
                Log.d(TAG, "needCloudSmartTipPopup() ] Cloud Smart Tip can show.");
                ret = true;
            }
        }
        return ret;
    }

    public static boolean planToDrawCloudSmartTip(Context context) {
        boolean ret = false;

        if (!AppFeatures.isSupportSamsungDrive() || PreferenceUtils.getCloudVisitFlag(context))
            return ret;

        int previousTipState = PreferenceUtils.getPreviousTipState(context);
        int previousTipType = PreferenceUtils.getPreviousTipType(context);
        return ret;
    }

    private static boolean isCloudTipOnlyEnabled(Context context) {
        boolean ret = false;

        if ((PreferenceUtils.getGmsVisitFlag(context) || SmartTipUtils.getSmartTipConditionNumber(context, AppConstants.SmartTipType.GMS_CHECK) == -1)
                && (SmartTipUtils.getSmartTipConditionNumber(context, AppConstants.SmartTipType.CLOUD_CHECK) != -1)) {
            ret = true;
        }
        return ret;
    }

    public static int getSmartTipConditionNumber(Context context, int tipType) {
        int conditionType = -1;
        long totalSize = StorageMonitor.getInternalTotalSize(context);
        long freeSpace = StorageMonitor.getStorageFreeSpace(context, StorageMonitor.iStorageType.INTERNAL);

        Log.d(TAG, "getSmartTipConditionNumber() ] Total Size = " + totalSize / FileUtils.GIGA_BYTES
                + " GB , Free Space = " + freeSpace / FileUtils.GIGA_BYTES + " GB");
        Log.d(TAG, "getSmartTipConditionNumber() ] Free space of Internal Storage is lower than " + Math.round(freeSpace * 100.0 / totalSize) + " %.");
        boolean conditionConsumed[] = new boolean[]{false, false, false};
        if (tipType == AppConstants.SmartTipType.CLOUD || tipType == AppConstants.SmartTipType.CLOUD_CHECK) {
            conditionConsumed = PreferenceUtils.getCloudTipConditionPool(context);
        } else if (tipType == AppConstants.SmartTipType.GMS || tipType == AppConstants.SmartTipType.GMS_CHECK) {
            conditionConsumed = PreferenceUtils.getGmsTipConditionPool(context);
        }
        Log.d(TAG, "getSmartTipConditionNumber() ] BEFORE : " + tipType + " (GMS = 0, CLOUD = 1) , ( "
                + conditionConsumed[0] + " , " + conditionConsumed[1] + " , " + conditionConsumed[2] + " )");
        if (freeSpace <= (long) (totalSize * 0.4) && !conditionConsumed[0]) {
            conditionType = 1;
            conditionConsumed[0] = true;
        } else if (freeSpace <= (long) (totalSize * 0.1) && !conditionConsumed[1]) {
            conditionType = 2;
            conditionConsumed[1] = true;
        } else if (freeSpace <= FileUtils.GIGA_BYTES && !conditionConsumed[2]) {
            conditionType = 3;
            conditionConsumed[2] = true;
        }
        if (tipType == AppConstants.SmartTipType.CLOUD) {
            PreferenceUtils.setCloudTipConditionPool(context, conditionConsumed);
        } else if (tipType == AppConstants.SmartTipType.GMS) {
            PreferenceUtils.setGmsTipConditionPool(context, conditionConsumed);
        }
        Log.d(TAG, "getSmartTipConditionNumber() ] AFTER : " + tipType + " (GMS = 0, CLOUD = 1) , ( "
                + conditionConsumed[0] + " , " + conditionConsumed[1] + " , " + conditionConsumed[2] + " )");
        Log.d(TAG, "getSmartTipConditionNumber() ] conditionType = " + conditionType);
        return conditionType;
    }

    public static void saveGmsPortraitLocation(Context context, View view) {
        if (view != null) {
            int currentLocation[] = new int[2];
            Rect rect = new Rect();
            view.getGlobalVisibleRect(rect);
            currentLocation[0] = rect.width() - changeDpToPx(context, SMART_TIP_WIDTH_MARGIN);
            currentLocation[1] = changeDpToPx(context, STATUS_BAR_HEIGHT + SMART_TIP_HEIGHT_MARGIN);
            PreferenceUtils.setGmsPortraitLocation(context, currentLocation);
            Log.d(TAG, "saveGmsPortraitLocation() ] rect X = " + currentLocation[0] + " , rect Y = " + currentLocation[1]);
        }
    }

    public static void saveGmsLandscapeLocation(Context context, View view) {
        if (view != null) {
            int currentLocation[] = new int[2];
            int height = SMART_TIP_HEIGHT_MARGIN;
            if (AppFeatures.isTabletUIMode()) {
                height += STATUS_BAR_HEIGHT;
            }
            Rect rect = new Rect();
            view.getGlobalVisibleRect(rect);
            currentLocation[0] = rect.width() - changeDpToPx(context, SMART_TIP_WIDTH_MARGIN);
            currentLocation[1] = changeDpToPx(context, height);
            PreferenceUtils.setGmsLandscapeLocation(context, currentLocation);
            Log.d(TAG, "saveGmsLandscapeLocation() ] rect X = " + currentLocation[0] + " , rect Y = " + currentLocation[1]);
        }
    }

    private static int changeDpToPx(Context context, int dp) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()));
    }
}
